<?php
/*
 * @分销商城
 * @功能说明：首页栏目
 * @更新说明：暂无更新
 * @文件名 IndexController.class.php
 * @编码 UTF-8
 * @创建时间 2016-3-7 下午12:32:58
 * @创建人 XING
 */

namespace wap_cash\Controller;
class IndexController extends ApibaseController
{
    /*
	 * 分销商城_首页模块
	 * @date: 2016-3-7 下午12:33:15
	 * @editor: XING
	 */
    public function index(){

        $this->display('index/index');
    }

    /*
     * π付系统-充值列表接口
     * @date: 2017-12-14 下午16:33:15
     * @editor: yanyan
     */
    public function rechargeList(){
        $perpage = I('post.perpage')? : 20;
        $page = I('post.page')? : 1;
        $list = M('UserRecharge')->order(['add_at' => 'desc'])->page($page, $perpage)->select();
        if($list){
            dataFormat($list,1,'success');
        }else{
            dataFormat('',0,'error');
        }

    }

    /*
     * π付系统-充值列表详情页接口
     * @date: 2017-12-14 下午16:33:15
     * @editor: yanyan
     */
    public function rechargeDetail(){
        if(!I('post.recharge_id')){
            jsonRespons('0','参数缺失');
        }
        $recharge_id = I('post.recharge_id');
        $list_one = M('UserRecharge')->where(['recharge_id' => $recharge_id])->find();
        if($list_one){
            dataFormat($list_one,1,'success');
        }else{
            dataFormat('',0,'error');
        }

    }

    /*
     * π付系统-消费套餐列表接口
     * @date: 2017-12-14 下午16:33:15
     * @editor: yanyan
     */
    public function mealList(){
        $perpage = I('post.perpage')? : 20;
        $page = I('post.page')? : 1;
        $list = M('OrderEntity oe')
            ->join('pf_project_entity pe ON oe.project_id = pe.project_id','LEFT')
            ->field('oe.id,oe.order_id,oe.user_id,oe.user_name,oe.order_amount,oe.user_mobile,oe.is_used,oe.score_type,oe.consumer,oe.consumer_mobile,oe.class_room,oe.servicer,oe.counselor,oe.operate_nurse,oe.operate_place,oe.add_at,oe.use_at,pe.project_name,pe.cover_img,pe.project_id')
            ->where(['oe.is_used'=>1])->order(['add_at' => 'desc'])->page($page, $perpage)->select();
        if($list){
            dataFormat($list,1,'success');
        }else{
            dataFormat('',0,'error');
        }
    }
    /*
     * π付系统-充值列表详情页接口
     * @date: 2017-12-14 下午16:33:15
     * @editor: yanyan
     */
    public function mealListDetail(){
        if(!I('post.order_id')){
            jsonRespons('0','参数缺失');
        }
        $order_id = I('post.order_id');
        $list_one = M('OrderEntity oe')
            ->join('pf_project_entity pe ON oe.project_id = pe.project_id','LEFT')
            ->field('oe.id,oe.order_id,oe.user_id,oe.user_name,oe.order_amount,oe.user_mobile,oe.is_used,oe.score_type,oe.consumer,oe.consumer_mobile,oe.class_room,oe.servicer,oe.counselor,oe.operate_nurse,oe.operate_place,oe.add_at,oe.use_at,pe.project_name,pe.cover_img,pe.project_id')
            ->where(['oe.is_used'=>1,'oe.order_id'=>$order_id])->find();
        if($list_one){
            dataFormat($list_one,1,'success');
        }else{
            dataFormat('',0,'error');
        }

    }

    /**
     * 充值审核
     * create by: liuqi
     * Date:2018-1-9 16:10:02
     */
    public function checkRecharge()
    {

            if (I('post.recharge_id')) {
                $chargeModel = M('UserRecharge');
                $userModel = M('UserInfo');
                $scoreDetail = M('ScoreDetail');
                $chargeModel->startTrans();
                $userModel->startTrans();
                $scoreDetail->startTrans();
                $chargeInfo = $chargeModel->lock(true)->where(['recharge_id' => I('post.recharge_id'),'is_check'=>0,'check_at'=>0])->field('user_id,score_type,recharge_money')->find();
                if ($chargeInfo) {
                    $set = $chargeModel->lock(true)->where(['recharge_id' => I('post.recharge_id'),'is_check'=>0,'check_at'=>0])->setField(['is_check' => 1, 'operator' => session('loginName'), 'remark' => trim(I('post.remark')), 'update_at' => time(), 'check_at' => time()]);
                    if ($set) {
                        switch ($chargeInfo['score_type']) {
                            case '1':
                                $score_type = 'ordinary_score';
                                break;
                            case '2':
                                $score_type = 'platina_score';
                                break;
                            case '3':
                                $score_type = 'diamonds_score';
                                break;
                            case '4':
                                $score_type = 'experience_score';
                                break;
                            case '5':
                                $score_type = 'gold_score';
                                break;
                            case '6':
                                $score_type = 'mouth_score';
                                break;
                            default:
                                break;
                        }
                        $update = $userModel->lock(true)->where(['user_id' => $chargeInfo['user_id']])->setInc($score_type, $chargeInfo['recharge_money']);
                        if ($update) {
                            $inset = $scoreDetail->lock(true)->add(['user_id' => $chargeInfo['user_id'], 'detail_type' => 1, 'detail_score' => $chargeInfo['recharge_money'], 'score_type' => $chargeInfo['score_type'], 'remark' => '充值', 'add_at' => time(), 'update_at' => time()]);
                            if ($inset) {
                                if($chargeInfo['score_type'] == 1){
                                    //充值的积分为普通积分，增加相应的体验积分
                                    $exp = $userModel->lock(true)->where(['user_id' => $chargeInfo['user_id']])->setInc('experience_score', $chargeInfo['recharge_money']);
                                    if($exp){
                                        $ordinaryDetail = $scoreDetail->lock(true)->add(['user_id' => $chargeInfo['user_id'], 'detail_type' => 1, 'detail_score' => $chargeInfo['recharge_money'], 'score_type' => 4, 'remark' => '充值', 'add_at' => time(), 'update_at' => time()]);
                                        if($ordinaryDetail){
                                            $scoreDetail->commit();
                                            $chargeModel->commit();
                                            $userModel->commit();
                                            jsonRespons('1','审核通过');
                                        }else{
                                            $chargeModel->rollback();
                                            $scoreDetail->rollback();
                                            $userModel->rollback();
                                            jsonRespons('0','审核失败[-2]');
                                        }
                                    }else{
                                        $chargeModel->rollback();
                                        $scoreDetail->rollback();
                                        $userModel->rollback();
                                        jsonRespons('0','审核失败[-1]');
                                    }
                                }
                                $scoreDetail->commit();
                                $chargeModel->commit();
                                $userModel->commit();
                                jsonRespons('1','审核通过');
                            } else {
                                $chargeModel->rollback();
                                $scoreDetail->rollback();
                                $userModel->rollback();
                                jsonRespons('0','审核失败[-3]');
                            }
                        } else {
                            $userModel->rollback();
                            $chargeModel->rollback();
                            jsonRespons('0','审核失败[0]');
                        }
                    } else {
                        jsonRespons('0','审核失败[1]');
                    }
                } else {
                    jsonRespons('0','审核失败[2]');
                }
            }else{
                jsonRespons('0','参数缺失');
            }
    }

    /**
     * 取消审核
     */
    public function delRecharge()
    {
        if(I('post.recharge_id')){
            $info = M('UserRecharge')->where(['recharge_id'=>I('post.recharge_id')])->find();
            if($info['is_check'] == '0'){
                $del = M('UserRecharge')->where(['recharge_id'=>I('post.recharge_id')])->setField(['is_check'=>-1,'remark'=>trim(I('post.remark')),'update_at'=>time(),'check_at'=>time()]);
                if($del){
                    jsonRespons('1','取消成功');
                }
            }
        }else{
            jsonRespons('0','参数缺失');
        }
    }

    //指向页面
    public function recharge_list(){
        $this->display('index/recharge_list');
    }
    public function meal_list(){
        $this->display('index/meal_list');
    }
    public function recharge_info(){
        $this->display('index/recharge_info');
    }
    public function meal_info(){
        $this->display('index/meal_info');
    }

}
